package com.ff.discounts.component.address

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.Menu
import android.view.MenuItem
import androidx.activity.viewModels
import androidx.lifecycle.ViewModelProvider
import androidx.lifecycle.lifecycleScope
import com.ff.discounts.R
import com.ff.discounts.activity.BaseTitleActivity
import com.ff.discounts.component.addressdetail.AddressDetailActivity
import com.ff.discounts.component.login.LoginViewModel
import com.ff.discounts.databinding.ActivityAddressBinding
import com.ff.discounts.library.log.HiLog
import com.ff.discounts.util.Constant
import kotlinx.coroutines.launch

/**
  *
  * @description 收货地址界面
  * @author
  * @time 2023-10-29 13:56
  */

class AddressActivity : BaseTitleActivity<ActivityAddressBinding>() {
    private lateinit var addressViewModel: AddressViewModel
    private lateinit var adapter: AddressAdapter
    override fun initDatum() {
        super.initDatum()
        val viewModelFactory = AddressViewModelFactory(extraInt(Constant.STYLE))
        addressViewModel = ViewModelProvider(this, viewModelFactory)[AddressViewModel::class.java]
        initViewModel(addressViewModel)
        adapter = AddressAdapter()
        binding.list.adapter = adapter
        lifecycleScope.launch {
            addressViewModel.data
                .collect { data ->
                    adapter!!.submitList(data)
                }
        }
    }

    /**
     * 返回菜单
     */
    override fun onCreateOptionsMenu(menu: Menu?): Boolean {
        menuInflater.inflate(R.menu.menu_add,menu)
        return true
    }

    /**
     * 菜单点击事件
     */
    override fun onOptionsItemSelected(item: MenuItem): Boolean {
        if(item.itemId == R.id.add){
            // 添加按钮
            onAddClick()
            return true
        }
        return super.onOptionsItemSelected(item)
    }

    private fun onAddClick() {
        startActivity(AddressDetailActivity::class.java)
    }


    override fun onResume() {
        super.onResume()
        addressViewModel.loadData()
    }

}